using System.Activities.Presentation;
using Headsight.BPMN2WF.BPMN.Model;
using Headsight.BPMN2WF.BPMN.Schemas;
using Headsight.BPMN2WF.Testing.TestObjCtors;
using Headsight.BPMN2WF.Transformation.BPMN2TransformerMappings;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BPMNFlowNodesBlockFactory = Headsight.BPMN2WF.BPMN.BPMNFlowNodesBlockFactory;

namespace Headsight.BPMN2WF
{
    [TestClass]
    public class BPMN2WFTest
    {
        private IBPMN2WF _sut;

        [TestInitialize]
        public void SetUp()
        {
            _sut = BPMN2WFFactory.Create(new BPMN2WFTransformationVNext(), true, null);
        }

        private static BPMNFlowNodesBlock CreateBPMNModel(ITestObjCtor<tProcess> tProcessCtor)
        {
            var tDefinitions = new tDefinitionsCtor().With(tProcessCtor.Create()).Create();

            return new BPMNFlowNodesBlockFactory()
                .CreateBPMNFlowNodesBlock(tDefinitions);
        }

        private void PerformTransformation(BPMNFlowNodesBlock bpmnProcess)
        {
            var result = _sut.Transform(bpmnProcess);

            var wfDesigner = (WorkflowDesigner)result.Model;

            var fileName = Properties.Settings.Default.TestingRootDir + @"\Headsight.BPMN2WFTest.Workflows\Models\BPMN2WFImpl2.xaml";
            wfDesigner.Save(fileName);
        }

        [TestMethod]
        public void Transform_inco_exclusiveGateway_diverging_and_converging()
        {
            var bpmnModel = CreateBPMNModel(new tProcessCtor()
                .WithStartEvent("start")
                .Flow("start", "gateway_id")
                .Branch("gateway_id", "branch_1_id")
                .Branch("gateway_id", "branch_2_id")
                .Flow("branch_1_id", "gateway_end")
                .Flow("branch_2_id", "gateway_end")
                );

            PerformTransformation(bpmnModel);
        }

        [TestMethod]
        public void Transform_inco_exclusiveGateway_diverging_and_converging_exl_gateway_diverging_and_converging()
        {
            var bpmnModel = CreateBPMNModel(new tProcessCtor()
                .WithStartEvent("start")
                .Flow("start", "gateway_id")
                .Branch("gateway_id", "branch_1_id")
                .Branch("gateway_id", "branch_2_id")
                .Flow("branch_1_id", "gateway_end")
                .Flow("branch_2_id", "gateway_end")
                .Flow("gateway_end", "gateway_start2")
                .Branch("gateway_start2", "gateway2_branch1")
                .Branch("gateway_start2", "gateway2_branch2")
                .Flow("gateway2_branch1", "gateway2_end")
                .Flow("gateway2_branch2", "gateway2_end")
                .Flow("gateway2_end", "workflow_end")
                );

            PerformTransformation(bpmnModel);
        }

        [TestMethod]
        public void Transform_inco_exclusiveGateway_diverging_and_converging_exl_gateway_diverging_and_converging_()
        {
            var bpmnModel = CreateBPMNModel(new tProcessCtor()
                .WithStartEvent("start")
                .Flow("start", "gateway_id")
                .Branch("gateway_id", "branch_1_id")
                .Branch("gateway_id", "branch_2_id")
                .Flow("branch_1_id", "gateway_end")
                .Flow("branch_2_id", "gateway_end")
                .Flow("gateway_end", "abc")
                .Branch("abc", "gateway2_branch1")
                .Branch("abc", "gateway2_branch2")
                .Flow("gateway2_branch1", "gateway2_end")
                .Flow("gateway2_branch2", "gateway2_end")
                .Flow("gateway2_end", "workflow_end")
                );

            PerformTransformation(bpmnModel);
        }


        [TestMethod]
        public void Transform_inco_exclusiveGateway_()
        {
            var bpmnModel = CreateBPMNModel(new tProcessCtor()
                .WithStartEvent("start")
                .Flow("start", "gateway_id")
                .Branch("gateway_id", "branch_1_id")
                .Branch("gateway_id", "branch_2_id")
                .Flow("branch_1_id", "branch_1_node_1")
                .Flow("branch_1_node_1", "branch_1_node_2"));

            PerformTransformation(bpmnModel);
        }

        [TestMethod]
        public void Transform_inco_sequential_3()
        {
            var bpmnModel = CreateBPMNModel(
                new tProcessCtor()
                    .WithStartEvent("start")
                    .Flow("start", "node1")
                    .Flow("node1", "node2")
                );

            PerformTransformation(bpmnModel);
        }

        [TestMethod]
        public void TransformIncoExclusiveGatewayDiverging()
        {
            var bpmnModel = CreateBPMNModel(new tProcessCtor()
                .WithStartEvent("start")
                .Flow("start", "gateway_id")
                .Branch("gateway_id", "branch_1_id")
                .Branch("gateway_id", "branch_2_id"));

            PerformTransformation(bpmnModel);
        }

    }
}